package cinema.web;

import java.io.IOException;
import java.util.Date;

import java.util.Iterator;
import java.util.ListIterator;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cinema.dao.CinemasHibernate;
import cinema.model.Cinemas;
import cinema.model.Timeslot;

/**
 * Servlet implementation class CinemaTestServlet
 */
public class CinemaTestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CinemaTestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		  
		CinemasHibernate CinHiber = new CinemasHibernate();
		
		//Delete Cinema
		
		Cinemas cinDelete = CinHiber.getCinemabyID(2);
		
		if(cinDelete != null){
			if(CinHiber.deleteCinemas(cinDelete)){
				// delete success
			}
			else{
				// delete unsuccessful
			}
		}
		else{
			//the cinema you trying to delete is not there
		}
		
		//Insert Cinema
		Cinemas cinsert = new Cinemas();
		cinsert.setId(2);
		cinsert.setName("Test Insert");
		cinsert.setAddress("Test insert 2");
		CinHiber.saveCinemas(cinsert);
		
		//Select Based on ID
		Cinemas cin = CinHiber.getCinemabyID(2);
		String a = cin.getName();
		String b = cin.getAddress();
		
		//Update Cinema using selected cinemas above
		cin.setName("Test Update");
		cin.setAddress("Test Update 2");
		CinHiber.saveCinemas(cin);
		
		//Select All
		ListIterator<Cinemas> it = CinHiber.getCinemas();
		
		
		while(it.hasNext()){
			Cinemas act = (Cinemas) it.next();
			
			String Address = act.getAddress();
			String Name = act.getName();
			
			Iterator<Timeslot> iter = act.getTimeslots().iterator();
			
	        
			
			while(iter.hasNext()){
				Timeslot slot = iter.next();
				
				Date starttime = slot.getStarttime();
				Date endtime = slot.getEndtime();
			}
			
			
			
		}
        
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
